package com.zzyl.member.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzyl.member.domain.Reservation;
import com.zzyl.member.dto.ReservationCountDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 预约信息Mapper接口
 *
 * @author CHEN
 * @date 2025-07-31
 */
@Mapper
public interface ReservationMapper extends BaseMapper<Reservation> {


    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息集合
     */
    public List<Reservation> selectReservationList(Reservation reservation);


    /**
     * 查询当天取消预约次数
     *
     * @return
     */
    int getCancelledCount(Long userId);

    /**
     * 查询预约信息列表
     *
     * @param localDateTime
     * @return
     */
    List<ReservationCountDTO> countByTime(LocalDateTime localDateTime, LocalDateTime localDateTime1);


    /**
     * 修改预约信息状态为取消
     *
     * @param id
     * @return
     */
    int updateStatusToCancel(Long id);

    // 查询已过期的预约信息
    @Select("select * from reservation where status = '0' and time < #{now}")
    List<Reservation> findExpired(@Param("now") LocalDateTime now);

    // 批量修改预约信息状态为取消
    @Update("update reservation set status = '3' where id in (${ids})")
    int batchMarkExpired(@Param("ids") String ids);
    // 方便定时任务一次搞定
    default List<Reservation> findAndMarkExpired(LocalDateTime now) {
        List<Reservation> list = findExpired(now);
        if (!list.isEmpty()) {
            String ids = list.stream()
                    .map(r -> r.getId().toString())
                    .collect(Collectors.joining(","));
            batchMarkExpired(ids);
        }
        return list;
    }

//    /**
//     * 查询每个时间段剩余预约次数（每个时间段最多六次）
//     * @return
//     */
//    String getTimingCount();
}
